//
//  ViewController.m
//  testSeeScoremy
//
//  Created by JM(jieson) on 16/3/11.
//  Copyright © 2016年 mj. All rights reserved.
//

#import "ViewController.h"
#import <SeeScoreLib/SeeScoreLib.h>
#import <SeeScoreLib/SSScore.h>
#import "jm.h"

#define kMarginX 10

// licence keys: contents, transpose, item_colour, multipart, ios, osx
static const sscore_libkeytype skey = {"evaluation", {0X3494,0X0}, {0X41a8d1dd,0X326a25d8,0X6a66f439,0X56daa1c3,0X2bb235c1,0X1af29574,0X40e72850,0X807bd7a,0X2d66e3c5,0X342898b4,0X55416c2c,0Xe1913f9c,0X3a24e86e,0X4d17ab94,0X1eb7ca0e}};
const sscore_libkeytype *const sscore_libkey = &skey;

@interface ViewController ()
{
    SSScore *score;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    NSArray *sampleFileUrls = [[NSBundle mainBundle] URLsForResourcesWithExtension:@"mxl" subdirectory:@""];
    
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"ActorPreludeSample" withExtension:@"mxl"];
    
    NSString *fileName = [[fileUrl path] lastPathComponent];
    NSLog(@"filename:%@",fileName);
  
    
    SSLoadOptions *loadOptions = [[SSLoadOptions alloc] initWithKey:sscore_libkey];
    loadOptions.checkxml = true;
    sscore_loaderror err;
    score = [SSScore scoreWithXMLFile:[fileUrl path] options:loadOptions error:&err];
    if (err.err == sscore_NoError) {
        NSLog(@"score:%d",score.numBars);
    }else {
        NSLog(@"score err:%d",err.err);
    }
    
    //
    int numParts = score.numParts;
    NSMutableArray *showingParts = [NSMutableArray arrayWithCapacity:numParts];
    for (int i = 0; i < numParts; ++i)
    {
        [showingParts addObject:[NSNumber numberWithBool:YES]];// display all parts
    }
    
    //
    SSLayoutOptions *layOptions = [[SSLayoutOptions alloc] init];
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,10), YES/*opaque*/, 0.0/* scale*/);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    enum sscore_error err2 =
    [score layoutWithContext:ctx
                       width:self.view.frame.size.width - 2 * kMarginX maxheight:self.view.frame.size.height
                       parts:showingParts magnification:20*1
                     options:layOptions
                    callback:^bool (SSSystem *sys){
                        // callback is called for each new laid out system
                        // return false if abort required
                        
//                        if (self.abortingBackground == 0)
//                        {
                            dispatch_sync(dispatch_get_main_queue(), ^{
//                                if (self.abortingBackground == 0)
                                {
//                                    [self addSystemToList:sys];
//                                    [self setNeedsLayout];
//                                    if (self.scrollDelegate)
//                                        [self.scrollDelegate update]; // update the barcontrol to show more bars loaded
                                }
                            });
                            return true;
//                        }
                        else
                            return false;}];
    UIGraphicsEndImageContext();
    switch (err2)
    {
        case sscore_NoError:break;
        case sscore_OutOfMemoryError:	NSLog(@"out of memory");break;
        case sscore_XMLValidationError: NSLog(@"XML validation error");break;
        case sscore_NoBarsInFileError:	NSLog(@"No bars in file error");break;
        case sscore_WidthTooSmallError: NSLog(@"WidthTooSmall Error"); break;
        case sscore_NullGraphicsError:	NSLog(@"NullGraphics Error"); break;
        case sscore_MagnificationTooSmallError:	NSLog(@"MagnificationTooSmall Error"); break;
        case sscore_MagnificationTooLargeError:	NSLog(@"MagnificationTooLarge Error"); break;
        case sscore_NoPartsError:			NSLog(@"NoParts Error"); break;
        case sscore_NoPartsToDisplayError:	NSLog(@"NoPartsToDisplay Error"); break;
        default:
        case sscore_UnknownError:		NSLog(@"Unknown error");break;
        case sscore_BadHeightError:		NSLog(@"bad height");break;
        case sscore_WidthTooLargeForIphoneError:	NSLog(@"the system width is limited on the iPhone-only licensed framework");break;
        case sscore_HeightTooLargeForIphoneError:	NSLog(@"the system height is limited on the iPhone-only licensed framework");break;
    }
    assert(err2 == sscore_NoError);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(void)setupScore:(SSScore*)sc openParts:(NSArray *)parts mag:(float)mag opt:(SSLayoutOptions *)options
//{
//    assert(sc);
//    assert(parts.count > 0);
//    // abort any existing layout/draw...
//    [self abortBackgroundProcessing:^{ // .. and on completion:
//        assert(!layoutProcessing);
//        [self clearAll];
//        displayingParts = parts;
//        score = sc;
//        _magnification = mag;
//        [systemlist removeAllObjects];
//        CGRect frame = self.frame;
//        assert(frame.size.width > 0);
//        // we want a smaller scaling for smaller screens (iphone), but less than proportionate
//        magnificationScalingForWidth = [self magnificationScaling:frame.size.width];
//        if (self.abortingBackground == 0)
//        {
//            assert(!layoutProcessing);
//            layoutProcessing = true;
//            assert(systemlist.count == 0);
//            dispatch_async(background_layout_queue, ^{
//                assert(systemlist.count == 0);
//                if (self.abortingBackground == 0)
//                {
//                    UIGraphicsBeginImageContextWithOptions(CGSizeMake(10,10), YES/*opaque*/, 0.0/* scale*/);
//                    CGContextRef ctx = UIGraphicsGetCurrentContext();
//                    enum sscore_error err = [score layoutWithContext:ctx
//                                                               width:frame.size.width - 2 * kMarginX maxheight:frame.size.height
//                                                               parts:parts magnification:self.magnification * magnificationScalingForWidth
//                                                             options:options
//                                                            callback:^bool (SSSystem *sys){
//                                                                // callback is called for each new laid out system
//                                                                // return false if abort required
//                                                                
//                                                                if (self.abortingBackground == 0)
//                                                                {
//                                                                    dispatch_sync(dispatch_get_main_queue(), ^{
//                                                                        if (self.abortingBackground == 0)
//                                                                        {
//                                                                            [self addSystemToList:sys];
//                                                                            [self setNeedsLayout];
//                                                                            if (self.scrollDelegate)
//                                                                                [self.scrollDelegate update]; // update the barcontrol to show more bars loaded
//                                                                        }
//                                                                    });
//                                                                    return true;
//                                                                }
//                                                                else
//                                                                    return false;}];
//                    UIGraphicsEndImageContext();
//                    switch (err)
//                    {
//                        case sscore_NoError:break;
//                        case sscore_OutOfMemoryError:	NSLog(@"out of memory");break;
//                        case sscore_XMLValidationError: NSLog(@"XML validation error");break;
//                        case sscore_NoBarsInFileError:	NSLog(@"No bars in file error");break;
//                        case sscore_WidthTooSmallError: NSLog(@"WidthTooSmall Error"); break;
//                        case sscore_NullGraphicsError:	NSLog(@"NullGraphics Error"); break;
//                        case sscore_MagnificationTooSmallError:	NSLog(@"MagnificationTooSmall Error"); break;
//                        case sscore_MagnificationTooLargeError:	NSLog(@"MagnificationTooLarge Error"); break;
//                        case sscore_NoPartsError:			NSLog(@"NoParts Error"); break;
//                        case sscore_NoPartsToDisplayError:	NSLog(@"NoPartsToDisplay Error"); break;
//                        default:
//                        case sscore_UnknownError:		NSLog(@"Unknown error");break;
//                        case sscore_BadHeightError:		NSLog(@"bad height");break;
//                        case sscore_WidthTooLargeForIphoneError:	NSLog(@"the system width is limited on the iPhone-only licensed framework");break;
//                        case sscore_HeightTooLargeForIphoneError:	NSLog(@"the system height is limited on the iPhone-only licensed framework");break;
//                    }
//                    assert(err == sscore_NoError);
//                }
//                layoutProcessing = false;
//            });
//        }
//    }];
//}

@end
